const args = require('minimist')(process.argv.slice(2));
const path = require('path');
const webpack = require('webpack');
const Parame = require('./public/constants');
const cssnano = require('cssnano');
const baseConfig = require('./baseConfig');
const Visualizer = require('webpack-visualizer-plugin');//生成一个webpack资源分析图表html文件
const chalk = require('chalk');

const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCSS = new ExtractTextPlugin('css/[name].css?[contenthash:8]');
const ScriptExtHtmlWebpackPlugin =require('script-ext-html-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Dashboard = require('webpack-dashboard');
const DashboardPlugin = require('webpack-dashboard/plugin');
const WebpackNotifierPlugin = require('webpack-notifier');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');


let config = Object.assign({}, baseConfig, {
  entry: {
    [Parame.reactLibraryName]:[
      path.join(Parame.rootPath, `dll/${Parame.reactLibraryName}.dll.js`)
    ],
    [Parame.mainJsName]: [
      path.join(Parame.srcPath, 'entry')
    ]

  },
  output:Object.assign({}, baseConfig.output, {
    filename: 'js/[name].js?[chunkhash:8]', //[chunkhash]为文件自身的哈希，[hash]为此次打包的统一哈希,当开启热加载(HRM)设置为chunkhash会报错
    jsonpFunction:'_$_$_'//webpack异步加载模块的函数名默认"webpackJsonp"
  }),
  cache: false,
  devtool: 'sourcemap',
  plugins: [].concat([
    extractCSS,
    new ProgressBarPlugin({
      format: ` build [${chalk.blue(':bar')}] ${chalk.green.bold(':percent')} (${chalk.red(':elapsed')} seconds)-:msg`,
      clear:true,
      renderThrottle:9,
    }),
    new WebpackNotifierPlugin({
      title: 'Webpack',
      message:'success'
    }),
    new ScriptExtHtmlWebpackPlugin({defer:[/.+/]}),
    new webpack.DllReferencePlugin({
      context: Parame.rootPath,
      /**
       * 在这里引入 manifest 文件
       */
      manifest: require(path.join(Parame.rootPath, `dll/${Parame.reactLibraryName}-manifest.json`)),
      sourceType :'var'
    }),
    new webpack.DefinePlugin({'process.env.NODE_ENV': '"production"'}),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap:true, //报错时能正确显示行+在统计信息中显示源
      compress: {
        warnings: false, //默认true; false为不显示warning级日志
        drop_console:false//默认为false; true为删除所有console.*的语句
      },
      max_line_len:1024*1024, //单行最大长度默认32000
      comments: ''//要保留的注释前缀
    }),
    //new webpack.optimize.AggressiveMergingPlugin({moveToParents:true}),//暂时关闭自动合并模块，为了体现分块的效果
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.LoaderOptionsPlugin({
      debug:false,
      minimize:true
    }),
    new HtmlWebpackPlugin(Object.assign({}, Parame.HtmlPluginConfig, {
      cache: false,
      chunksSortMode:function({names:[name1]}, {names:[name2]}){
        return Parame.mainJsName === name1;
      },
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true
      }
    }))
  ]),
  externals: {
    // 'redux': 'Redux',
    // 'react':'React',
    // 'react-dom':'ReactDOM',
    // 'react-redux': 'ReactRedux',
    // 'react-router': 'ReactRouter',
    'highcharts':'Highcharts',
    'moment': 'moment',
    'qs':'Qs'
  },
  stats:{
    timings:true,
    publicPath:true,
    reasons:true,
  }
});


if (args.profile){
  config.plugins.push(new Visualizer({filename: '../stats.html'}));
}
config.module.rules.push({
  test: /\.jsx?$/,
  loader: 'babel-loader',
  include: Parame.srcPath
}, {
  test: /ReactFamily/i,
  loader:'script-loader'
}, {
  test:/moment.*zh-cn/i,
  loader:'script-loader'
}, {
  test: /\.scss$/i,
  use: extractCSS.extract([
    {
      loader:'css-loader',
      options:{
        sourceMap:true,
        minimize:false,
      }
    },
    'postcss-loader',
    {loader:'sass-loader', options:{sourceMap:true}}
  ])
});


module.exports = config;
